/*
 * Copyright Konstantin Triger <kostat@gmail.com> 
 * 
 * This file is part of Jaque - JAva QUEry library <http://code.google.com/p/jaque/>.
 * 
 * Jaque is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jaque is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package jaque.misc;

/**
 * Static utilities.
 * 
 * @author <a href="mailto://object_streaming@googlegroups.com">Konstantin
 *         Triger</a>
 * 
 */
public final class Utils {
	private Utils() {
	}

	/**
	 * Returns the first nonnull value among its arguments.
	 * 
	 * @param <T>
	 *            the type of the objects to coalesce.
	 * @param first
	 *            first argument.
	 * @param second
	 *            second argument.
	 * @return Returns the first nonnull value among its arguments.
	 */
	public static <T> T coalesce(T first, T second) {
		return first != null ? first : second;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static byte defaultIfNull(Byte nullable, byte defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static short defaultIfNull(Short nullable, short defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static int defaultIfNull(Integer nullable, int defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static long defaultIfNull(Long nullable, long defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static float defaultIfNull(Float nullable, float defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null; otherwise,
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static double defaultIfNull(Double nullable, double defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

	/**
	 * Returns {@code nullable} value if it is not null, otherwise
	 * {@code defaultValue}.
	 * 
	 * @param nullable
	 *            Nullable parameter
	 * @param defaultValue
	 *            Value to return if nullable is null
	 * @return {@code nullable} value if it is not null; otherwise,
	 *         {@code defaultValue}.
	 */
	public static char defaultIfNull(Character nullable, char defaultValue) {
		return nullable != null ? nullable : defaultValue;
	}

}
